package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportExpense;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportExpenseDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-07 10:50:26
 */
public class TransportExpenseConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportExpenseConverts.class);

    public static List<TransportExpenseDTO> convert(List<TransportExpense> transportExpenseList) {
        if (CollectionUtils.isEmpty(transportExpenseList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportExpenseDTO> transportExpenseListResult = Lists.newArrayList();
        for (TransportExpense transportExpense : transportExpenseList) {
            transportExpenseListResult.add(convert(transportExpense));
        }
        return transportExpenseListResult;
    }

    public static TransportExpenseDTO convert(TransportExpense transportExpense) {
        try {
            return BeanUtils.copy(transportExpense, TransportExpenseDTO.class);
        } catch (Exception e) {
            LOG.error("TransportExpense到TransportExpenseDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportExpense到TransportExpenseDTO转换失败", e);
        }
    }

    public static TransportExpense convert(TransportExpenseDTO transportExpenseDTO) {
        try {
            return BeanUtils.copy(transportExpenseDTO, TransportExpense.class);
        } catch (Exception e) {
            LOG.error("TransportExpenseDTO到TransportExpense转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportExpenseDTO到TransportExpense转换失败", e);
        }
    }
}